<?php
/*
 * 仓库添加
 */

namespace app\adminbackstage\controller;
use app\BaseController;
use think\facade\Request;
use app\adminbackstage\model\Warehouse;


class WarehouseController extends BaseController{

    //仓库列表
    public function warehouseList(){
        $a_uid = Request::param('a_uid');
        $page = Request::param('page');
        $num = Request::param('num');
        
        $w_name = Request::param('w_name');
        $type = Request::param('type');

        $where = [];
        $where[] = ['enable','=','1'];
        if(!empty($w_name)){
            $where[] = ['w_name','like','%'.$w_name.'%'];
        }
        if(!empty($type)){
            $where[] = ['type','=',$type];
        }
        $list = Warehouse::warehouseList($where,$page,$num);
        if(!empty($list)){
            $list = $list->toArray();
            foreach ($list as $key => $value) {
                $list[$key]["addtime"] = date( "Y-m-d H:i:s",$value["addtime"]);
                
                    /*if(strpos($value['img'],"img.alicdn.com") !== false){
                        $storeList[$key]["img"] =  $value['img'];
                    }
                    elseif(strpos($value['img'],"http") !== false){
                        $storeList[$key]["img"] =  $value['img'];
                    }
                    else{
                        
                        $storeList[$key]["img"] =  Config::get('app.swimming_url').$value['img'];
                    }*/
            }
        }
        $count = Warehouse::warehouseCount($where);
        return json(['code' => 1, 'msg' => lang(''), 'data' => $list, 'count' => $count]);
    }
    
    //仓库添加
    public function warehouseAdd(){
        //$store = new Store();
        $a_uid = Request::param('a_uid');
        $enable = Request::param('enable');
        $w_name = Request::param('w_name');
        $type = Request::param('type');
        $province = Request::param('province');
        $city = Request::param('city');
        $district = Request::param('district');
        $address = Request::param('address');
        
        if(empty($enable)){
            $enable = 1;
        }
        if($enable == -1){
            $enable  = 0;
        }
        $time = time();
        $data = [

            'w_name' => $w_name,
            'enable' => $enable,
            'addtime' => $time,
            'type' => $type,
            'province' => $province,
            'city' => $city,
            'address' => $address,
            'district' => $district,
            'address' => $address,

        ];

        Warehouse::warehouseAdd($data);
        return json(['code' => 1, 'msg' => lang(''), 'data' => '', 'count' => '']);
    }
    
    //仓库详情
    public function warehouseInfo(){
        $a_uid = Request::param('a_uid');

        $id = Request::param('id');
  
        $where = [];
        $where[] = ['id','=',$id];

        $info = Warehouse::warehouseInfo($where);
        if(!empty($info)){
            $info = $info->toArray();

            $info["addtime"] = date( "Y-m-d H:i:s",$info["addtime"]);

            /*if(strpos($storeList['img'],"img.alicdn.com") !== false){
                $storeList["img"] =  $storeList['img'];
            }
            elseif(strpos($storeList['img'],"http") !== false){
                $storeList["img"] =  $storeList['img'];
            }
            else{

                $storeList["img"] =  Config::get('app.swimming_url').$storeList['img'];
            }*/
       
        }

        return json(['code' => 1, 'msg' => lang(''), 'data' => $info, 'count' => '' ]);
    }
    
    //仓库修改
    public function warehouseEdit(){

        $a_uid = Request::param('a_uid');
        $id = Request::param('id');
        $enable = Request::param('enable');
        $w_name = Request::param('w_name');
        $type = Request::param('type');
        $province = Request::param('province');
        $city = Request::param('city');
        $district = Request::param('district');
        $address = Request::param('address');
        
        $update = [];
        if(!empty($w_name)){
            $update['w_name'] = $w_name;
        }
        
        if(!empty($type)){
            $update['type'] = $type;
        }
        
        if(!empty($province)){
            $update['province'] = $province;
        }
        
        if(!empty($city)){
            $update['city'] = $city;
        }
        
        if(!empty($district)){
            $update['district'] = $district;
        }
        
        if(!empty($address)){
            $update['address'] = $address;
        }
        
        if($enable==-1){
            $update['enable'] = 0;
        }else{
            $update['enable'] = 1;
        }


        Warehouse::warehouseEdit($id,$update);
        return json(['code' => 1, 'msg' => lang(''), 'data' => '', 'count' => '']);
    }

}
